package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.entity.exam.AnswerInfoListBean;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @date on 2018/8/18 23:25
 * @email 909732184@qq.com
 * 考试选择题的答案接续适配器
 */
public class ExamSelectAnalysisAdapter extends RecyclerView.Adapter {
    private List<AnswerInfoListBean> data = new ArrayList<>();
    private String answerStr ;
    private Context context;
    private LayoutInflater layoutInflater;
    //正确答案的下标
    private int index = -1;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    private OnSelectExamItemClickListener onSelectExamItemClickListener;

    private int SelectColor,SelectedColor;


    public List<AnswerInfoListBean> getData() {
        return data;
    }

    public void setData(List<AnswerInfoListBean> data,String answerStr) {
        this.data = data;
        this.answerStr = answerStr;
    }

    public OnSelectExamItemClickListener getOnSelectExamItemClickListener() {
        return onSelectExamItemClickListener;
    }

    public void setOnSelectExamItemClickListener(OnSelectExamItemClickListener onSelectExamItemClickListener) {
        this.onSelectExamItemClickListener = onSelectExamItemClickListener;
    }

    public ExamSelectAnalysisAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        SelectColor = ContextCompat.getColor(context,R.color.LightGreen);
        SelectedColor = ContextCompat.getColor(context,R.color.gray_text);
    }

    public interface OnSelectExamItemClickListener{
        void onSelectExamItemClick(int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = layoutInflater.inflate(R.layout.select_exam_item,parent,false);
        return new ViewHolder(layoutInflater.inflate(R.layout.select_analysis_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        String[]  str = new String[]{"A","B","C","D","E","F","G","H","I"};
        viewHolder.position = position;
        // 根据isSelected来设置checkbox的选中状况
        if (answerStr.contains(position+"")) {
            viewHolder.tvChoose.setText(str[position]);
            viewHolder.tvText.setTextColor(SelectColor);
            viewHolder.tvChoose.setTextColor(SelectColor);
        } else {
            viewHolder.tvChoose.setText(str[position]);
            viewHolder.tvText.setTextColor(SelectedColor);
            viewHolder.tvChoose.setTextColor(SelectedColor);
        }
        viewHolder.tvText.setText(data.get(position).getDescription()+"");
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        public int position;
        private TextView tvText;
        private TextView tvChoose;
        public ViewHolder(final View itemView) {
            super(itemView);
            tvText = itemView.findViewById(R.id.tvText);
            tvChoose =  itemView.findViewById(R.id.tvChoose);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onSelectExamItemClickListener!=null){
                        onSelectExamItemClickListener.onSelectExamItemClick(position);
                    }
                }
            });
        }
    }
}
